<?php
class  Auth extends MY_Controller{
	
	function __construct()
	{
		parent::__construct();
	}
	
	function login (){
		if($this->checkUserStatus()){
			redirect('dashboard');
		}else{
			if($_POST){				
				if($_POST['remember'] = 1)
				{
					if($this->aauth->login($_POST['email'],$_POST['password'],TRUE))
					redirect ('dashboard');
					else
					$data['login_error'] = $this->aauth->get_errors_array();
				}else{
					if($this->aauth->login($_POST['email'],$_POST['password']))
					redirect ('dashboard');
					else
					$data['login_error'] = $this->aauth->get_errors_array();
				}
					
			}

			$data['title'] = $this->title('登陆页面');
			$this->load->view($this->theme.'/auth/login',$data);

		}
	}
	
	function logout()
	{
		$this->aauth->logout();
		redirect('login');
	}
	
    function forgotPassword()
    {
    	$data['title'] = $this->title('申请重置密码');
		$this->load->view($this->theme.'/auth/forgotPassword',$data);
    }


    function sendMail()
    {
    	if(!$_POST){
    		
    	}else{
    		$user = aauth_user::find_by_email($_POST['email']);
    		if(!$user){
    			$data['msgError'] = "电子邮件地址不存在.";
    		}else{
    			$this->aauth->remind_password($user->email);
    			$data['msgSuccess'] = "电子邮件已经发送到如下地址: ".$user->email;
    		}
    		
    		$data['title'] = $this->title('忘记密码');
    		$this->load->view($this->theme.'/auth/sendMail',$data);
    	}
    	
    }
    
	function resetPassword()
	{
		$user_id = $this->uri->segment(2);
		$ver_code = $this->uri->segment(3);
		$data['title'] = $this->title('重新设置您的密码');
		if(!$user_id || !$ver_code ){
			$this->load->view($this->theme.'/auth/resetPasswordError',$data);
		}else{			
				$reset = $this->aauth->reset_password($user_id, $ver_code);
				if($reset){
					$this->load->view($this->theme.'/auth/resetPassword',$data);
				}else{
					$this->load->view($this->theme.'/auth/resetPasswordError',$data);
				}
			
			}
		
    	
	}
}   